$(function(){
	
	$('#_start_menu_tree').treegrid({ 
		data:menusByUser,
		checkbox: false,
		rownumbers: false,
		showHeader:false,
		idField: 'id',
		treeField:'text', 
		columns:[[ 
		{title:'菜单名称',field:'text',width:200}, 
		{field:'opt',title:'操作',width:120,align:'center',
			formatter:function(value,rowData){ 
				//var rowDataStr = JSON.stringify(rowData);
				var _id = rowData.id;
				//一级菜单格式："id":"1"
				//二级菜单格式："id":"1_16"
				if(_id.indexOf('_')!=-1){
					var btn = '';
					btn += "<a id='a"+_id+"' style='color:white' class=\"_send2Desktop\" onclick=\"_send2Desktop('"+_id+"')\" href=\"javascript:void(0)\">发送到桌面</a> ";  
					return btn;
				}
				return "";
			}
		}
		]],
		onSelect:function(row){
			$(this).treegrid(row.state === 'closed' ? 'expand' : 'collapse', row.id);  
		},
		onClickRow:function(row){
			_openWindow(row);
		},
		rowStyler: function (value, row, index) {
            return '';//'background-color:#666;color:white';
        }
	});
	
	
	
	/**/
	try{
	$('#_start_menu_tree').treegrid('getPanel')
		.removeClass('lines-both lines-no lines-right lines-bottom')
		.addClass('panel-body1 lines-bottom');
	}catch(e){}
	
	//$('#_start_menu_tree').css("background", "blue");
	$('._send2Desktop').linkbutton({plain:true,iconCls:'icon-add'});  //给每行后的button加easyui样式
	
	
	_start_menu_tree_div_hide();
});

function _start_menu_tree_div_show(){
	//$("#startMenuCss").attr({href: "css/startmenu.css"});
	window.parent._start_menu_tree_div_show();
}

function _start_menu_tree_div_hide(){
	//hide开始菜单，写在jquery.app.js加载好直接hide不行，不知道为什么
	window.parent._start_menu_tree_div_hide();
	//$("#startMenuCss").attr({href: "css/startmenu1.css"});
}

/**
 * 打开dialog,显示二级菜单内容
 * @param row
 */
function _openWindow(row){
	//alert(JSON.stringify(row));
	var id = row.id;
	var text = row.text;
	var href = row.href;
	//二级菜单点击会隐藏
	if(id.indexOf('_')!=-1){
		_start_menu_tree_div_hide();
	}else{
		return;
	}
	
	var width = 1000;
	var height = 700;
	//居中显示
    var iframeWidth = $(this).width();
	var iframeHeight = $(this).height();
    var setWidth = (iframeWidth - width) / 2;
    var setHeight = (iframeHeight - height) / 2;
    
    var opt = {
		'href':href,
		'text':text,
		'iframe':true
    }
    window.parent.createwindow(opt);
	/*
    $.iframe.win({
	    url:href,
	    title:text,
        width:width,   
        height:height,   
        modal:false, 
        left:setWidth,
        top:setHeight 
    });
    */
}

/**
 * 保存用户桌面图标
 * @param _id
 */
function _send2Desktop(_id){
	$.each(menusByUser, function(indexF,father){
		$.each(father.children,function(indexS,son){
			if(son.id == _id){
				var sonId = son.id;
				var sonText = son.text;
				var sonHref = son.href;
				_sendAjax(sonId,sonText,sonHref);
				//
				return;
			}
		});
	});
	event.stopPropagation();
}

/**
 * ajax 保存用户桌面图标
 * @param sonId
 * @param sonText
 * @param sonHref
 */
function _sendAjax(sonId,sonText,sonHref){
	var data = {
		'menuId':sonId,
		'text':sonText,
		'href':sonHref
	}
	$.ajax({
		type:"POST",
		url:'desktopAjax_addMenu2Desktop.action',
		dataType:"json",
		data:data,
		success:function(msg){
			window.parent.showSuccessToast('showSuccessToast', "发送成功！");
			window.parent.refreshapp();
		},
		error:function(er){
			alert(er.responseText.trim());
		}
	});
}
